#!/bin/bash

# Alias: Jaeger admin API port
# Menu: Addons
# Description:
#  If receiving traces is configured, you can customize the admin API port here.

case "$1" in
    default)
        PORT=$("$OMD_ROOT"/lib/omd/next_free_port TRACE_JAEGER_ADMIN_PORT 14269)
        echo "$PORT"
        ;;
    choices)
        echo "@{NETWORK_PORT}"
        ;;
    set)
        PORT=$("$OMD_ROOT"/lib/omd/next_free_port TRACE_JAEGER_ADMIN_PORT "$2")

        if [ "$PORT" != "$2" ]; then
            echo "The port $2 is in use. I've choosen $PORT instead." >&2
            echo "$PORT"
        fi

        cat <<EOF >"$OMD_ROOT"/etc/jaeger/omd-admin-port.yaml
# Written by TRACE_JAEGER_ADMIN_PORT hook
---
service:
    telemetry:
        metrics:
            level: detailed
            readers:
              - pull:
                  exporter:
                    prometheus:
                      host: "[::1]"
                      port: $PORT
EOF
        ;;
    depends)
        [ "$CONFIG_TRACE_RECEIVE" = on ]
        ;;
esac
